﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AresCustomYacht.Code;

namespace AresCustomYacht
{
    public partial class P_BudgetMaterial : PageManager
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            InitializePageManager();
            if (Page.IsPostBack) return; //fire events


            Utility.ApplyMode((PageMode) _mode, new List<WebControl> { btnAdd, btnAddNShowReport }, new List<WebControl> { btnUpdate }, new List<WebControl> { btnDelete });
            Utility.DisplayPageMessage(this, lblMessage);

            //Get boatId anyway
            var boat = _bl.GetBoat(BoatId); //There must be boat Id in the QueryString, otherwise let exception bubble

            dfnBoatId.Text = BoatIdString;
            lblBoatName.Text = boat.BoatName;

            var taskIdString = Utility.GetQueryStringItem(this, "ParentId");

            if (_mode == 0 && !string.IsNullOrEmpty(taskIdString ))
            {
                var task = _bl.GetTask(int.Parse(taskIdString));
                dfnTaskId.Text = taskIdString;
                lblTaskName.Text = task.Code + "  " + task.Name;
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Insert();
            var linkAddress = "Mode=0&BoatId=" + BoatIdString ;
            //go to Trade instead. in this case you need to keep note of the selected Division
            if (dffBookmark.Checked && !string.IsNullOrEmpty(Utility.GetQueryStringItem(this, "Bookmark")))
            {
                if (dffBookmark.Checked && !String.IsNullOrEmpty(Utility.GetQueryStringItem(this,"Bookmark"))) //user wants to go to book mark and there is a bookmark
                    linkAddress += "&ParentId=" +
                                   int.Parse(Utility.GetQueryStringItem(this, "BookmarkId"));
                Response.Redirect(Utility.GetQueryStringItem(this, "Bookmark") + "?k=" + Utility.Encrypt(linkAddress));
               
            }
            else
            {
                Response.Redirect("P-Division.aspx?k=" + Utility.Encrypt(linkAddress));
            }
            

        }

        private void Insert()
        {
            var budgetMaterial = new Tracking();
            ReflectionCopier<Tracking>.CopyControlsToObject(budgetMaterial, dfnTaskId.Parent);
            budgetMaterial.TaskId = int.Parse(dfnTaskId.Text);
            budgetMaterial.BoatId = BoatId;
            _dal.AddTracking(budgetMaterial);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Main-Reports.aspx?k=" + Utility.Encrypt("Mode=0&BoatId=" + BoatIdString + "&Message=Cancelled"));
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("P-Division.aspx?k=" + Utility.Encrypt("Mode=0&BoatId=" + BoatIdString));
        }

        protected void btnAddNShowReport_Click(object sender, EventArgs e)
        {
            Insert();
            Response.Redirect("Main-Reports.aspx?k=" + Utility.Encrypt("Mode=0&BoatId=" + BoatIdString + "&Message=Cost added for " + lblTaskName.Text));
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

        }
    }
}